<template>
  <div id="staItemDictSeDiv">
    <Splitpanes style="height: 100%">
      <Pane size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <vxe-input style="width: 180px" v-model="searchForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="getStaItemDictList" @keyup="keyupEnter" @search-click="getStaItemDictList()"></vxe-input>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="tloading" @click="getStaItemDictList"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新建" @click="addStaItemDictDialog()"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="deleteStaItemDict"></vxe-button>
            <!-- <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="同步" :loading="tloading" @click="sysStaItemDict"></vxe-button> -->

          </template>
        </vxe-toolbar>

        <div class="modulTable">
          <vxe-table ref="staItemDictTableRef" class="mytable-scrollbar staItemDictTableClazz" border="inner" align="center" height="auto" :data="staItemDictList" :loading="tloading" @current-change="itemdictRowClick" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
            <vxe-column type="checkbox" width="30"></vxe-column>
            <vxe-column field="seq" title="项目序号" width="80"></vxe-column>
            <vxe-column field="itemNo" title="项目代码" width="80"></vxe-column>
            <vxe-column field="itemNa" title="项目名称" min-width="140"></vxe-column>
            <vxe-column field="itemEnCode" title="缩写" width="100"></vxe-column>
            <vxe-column field="unit" title="单位" width="60"></vxe-column>
            <vxe-column field="itemKind" title="结果类型" width="80">
              <template #default="{ row }">
                <span>{{ formatCommon(itemKindDict, row.itemKind) }}</span>
              </template>
            </vxe-column>
            <vxe-column field="itemClass" title="项目归类" width="60">
              <template #default="{ row }">
                <span>{{ formatCommon(itemClassDict, row.itemClass) }}</span>
              </template>
            </vxe-column>
            <vxe-column field="workGroup" title="工作组" width="60">
              <template #default="{ row }">
                <span>{{ formatCommon(workGroupList, row.workGroup) }}</span>
              </template>
            </vxe-column>
            <vxe-column field="instrClass" title="仪器大类" width="60">
              <template #default="{ row }">
                <span>{{ formatCommon(instrClassDict, row.instrClass) }}</span>
              </template>
            </vxe-column>
          </vxe-table>
          <vxe-pager align="left" min-height="32px" :current-page.sync="page.currentPage" :page-size.sync="page.pageSize" :total="page.totalResult" :layouts="[
                'PrevJump',
                'PrevPage',
                'Number',
                'NextPage',
                'NextJump',
                'Sizes',
                'FullJump',
                'Total'
              ]" @page-change="handleSizeChange($event)"></vxe-pager>
        </div>
      </Pane>
      <Pane size="50">
        <el-tabs type="border-card" v-model="activeName" @tab-click="tabHandleClick">
          <el-tab-pane label="基础信息" name="first">
            <vxe-toolbar perfect>
              <template #buttons>
                <vxe-button type="text" class="button-blue" @click="saveStaItemDict('staItemDictFormRef')" icon="vxe-icon-square-plus" content="保存"></vxe-button>
              </template>
            </vxe-toolbar>
            <el-scrollbar style="width: 100%; height: calc(100% - 40px)">
              <el-form ref="staItemDictFormRef" :model="staItemDictForm" :rules="staItemDictFormRule" class="form-input-m" label-width="100px" inline-message>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="工作组" prop="workGroup">
                      <el-select v-model="staItemDictForm.workGroup" filterable placeholder="请选择工作组" style="width: 200px">
                        <el-option v-for="workGroup in workGroupList" :key="workGroup.id" :label="workGroup.name" :value="workGroup.code"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="项目归类" prop="itemClass">
                      <el-select @keyup="keyupFormEnter($event,0)" ref="form0" v-model="staItemDictForm.itemClass" placeholder="请选择" style="width:200px">
                        <el-option v-for="item in itemClassDict" :key="item.code" :label="item.name" :value="item.code" />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="英文缩写">
                      <el-input @keyup="keyupFormEnter($event,1)" ref="form1" v-model="staItemDictForm.itemEnCode"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="项目代码" prop="itemNo">
                      <el-input @keyup="keyupEnter($event,2)" ref="form2" v-model="staItemDictForm.itemNo" @blur="inputItemCodeCode()"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="中文名称" prop="itemNa">
                      <el-input @keyup="keyupEnter($event,3)" ref="form3" v-model="staItemDictForm.itemNa" @blur="addInputCode()"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="英文名称">
                      <el-input @keyup="keyupEnter($event,4)" ref="form4" v-model="staItemDictForm.itemEnName"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="输入码">
                      <el-input @keyup="keyupEnter($event,5)" ref="form5" v-model="staItemDictForm.inputCode"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="报告代码" prop="reportCode">
                      <el-input @keyup="keyupEnter($event,6)" ref="form6" v-model="staItemDictForm.reportCode"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="结果类型">
                      <el-select @keyup="keyupEnter($event,7)" ref="form7" v-model="staItemDictForm.itemKind" placeholder="请选择" style="width:200px">
                        <el-option v-for="item in itemKindDict" :key="item.code" :label="item.name" :value="item.code" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="小数位数" @keyup="keyupEnter($event,8)" ref="form8">
                      <el-input-number @keyup="keyupEnter($event,8)" ref="form8" v-model="staItemDictForm.decPos" :min="0" :max="100" />
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="单位">
                      <el-input @keyup="keyupEnter($event,9)" ref="form9" v-model="staItemDictForm.unit"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="仪器大类" prop="instrClass">
                      <el-select @keyup="keyupEnter($event,10)" ref="form10" v-model="staItemDictForm.instrClass" placeholder="请选择" style="width:200px">
                        <el-option v-for="item in instrClassDict" :key="item.code" :label="item.name" :value="item.code" />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="基础项目代码" prop="basItemCode">
                      <el-input @keyup="keyupEnter($event,11)" ref="form11" v-model="staItemDictForm.basItemCode"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item prop="seq" label="顺序">
                      <el-input-number @keyup="keyupEnter($event,13)" ref="form13" v-model="staItemDictForm.seq" :min="1" :max="100000" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="是否通道号转译" prop="isTran">
                      <el-switch v-model="staItemDictForm.isTran" inline-prompt active-text="是" inactive-text="否" active-value="1" inactive-value="0"></el-switch>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-collapse v-model="activeNames" @change="handleCollapseChange">
                  <el-collapse-item name="1">
                    <template #title>
                      <!-- 使用 el-divider 作为标题 -->
                      <el-divider content-position="left"> <a>{{isExpanded==true?'展开':'隐藏'}}</a></el-divider>
                    </template>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="价格">
                          <el-input v-model="staItemDictForm.price"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="默认取值">
                          <el-input v-model="staItemDictForm.defaultVal"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="标准项目">
                          <el-input v-model="staItemDictForm.baseCode"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="急诊申请项目">
                          <SelectDataList ref="erFeeitemRef" :url="'/lis/fee/item/findFeeItemList'" :displayField="[
                          { title: '编码', props: 'feeCode' }, //列标题
                          { title: '名称', props: 'feeName' }
                        ]" :placeholder="'请选择'" :inputKeyName="'keyWords'" @selectDataRow="erFeeitemSelect" @clearValue="clearerFeeitemName" @sliceValue="sliceErFeeitemHandler" v-model="staItemDictForm.erFeeitemName" :isPage="false" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="统计代码">
                          <el-input v-model="staItemDictForm.countCode"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="备注">
                          <el-input v-model="staItemDictForm.remark"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="测试方法">
                          <el-input v-model="staItemDictForm.testMethod"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="是否启用">
                          <el-radio-group v-model="staItemDictForm.enable">
                            <el-radio :label="1">启用</el-radio>
                            <el-radio :label="0">禁用</el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <!-- ---------------------------------------------------------------------------------------- -->

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="计算用默认取值">
                          <el-input v-model="staItemDictForm.defCalcValue"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="编号归类">
                          <el-input v-model="staItemDictForm.numType"></el-input>
                        </el-form-item>
                      </el-col>

                    </el-row>

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="默认文字结果标志">
                          <el-input v-model="staItemDictForm.defTextResultFlag"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="样本质量">
                          <el-input v-model="staItemDictForm.sampleQuality"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="HIS对照代码">
                          <el-input v-model="staItemDictForm.hisItemCode"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="体检对照代码">
                          <el-input v-model="staItemDictForm.physItemCode"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="结果超出数">
                          <el-input v-model="staItemDictForm.outResultNum"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="结果超出稀释倍数">
                          <el-input v-model="staItemDictForm.outResultDiluFactor"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="允许CV(%)">
                          <el-input-number v-model="staItemDictForm.allowCvPerct" :min="0" :max="100" :step="0.1" />
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="允许TEa(%)">
                          <el-input-number v-model="staItemDictForm.allowTeaPerct" :min="0" :max="100" :step="0.1" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="工作量">
                          <el-input v-model="staItemDictForm.workLoad"></el-input>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="是否参与统计">
                          <el-radio-group v-model="staItemDictForm.isStatistics">
                            <el-radio :label="1">是</el-radio>
                            <el-radio :label="0">否</el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="是否报告">
                          <el-radio-group v-model="staItemDictForm.isReport">
                            <el-radio :label="1">是</el-radio>
                            <el-radio :label="0">否</el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="是否质控">
                          <el-radio-group v-model="staItemDictForm.isQc">
                            <el-radio :label="1">是</el-radio>
                            <el-radio :label="0">否</el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-collapse-item>
                </el-collapse>
                <!-- 参考范围 -->
                <el-divider content-position="left">参考范围</el-divider>
                <el-row :gutter="20">
                  <el-col :span="6">
                    <el-form-item label="下限">
                      <el-input v-model="staItemDictForm.lnum"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="6">
                    <el-form-item label="上限">
                      <el-input v-model="staItemDictForm.hnum"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="参考值">
                      <!-- <el-input v-model="form.ref"></el-input> -->
                      <vxe-textarea v-model="staItemDictForm.ref" style="width:100%;border-radius: 4px !important"></vxe-textarea>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="6">
                    <el-form-item label="结果标志:  <下限">
                      <el-select v-model="staItemDictForm.lflag" placeholder="请选择" clearable>
                        <el-option v-for="item in [{code:'N',name:'阴性'},{code:'P',name:'阳性'}]" :key="item.code" :label="item.name" :value="item.code" />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="6">
                    <el-form-item label=">上限">
                      <el-select v-model="staItemDictForm.hflag" placeholder="请选择" clearable>
                        <el-option v-for="item in [{code:'N',name:'阴性'},{code:'P',name:'阳性'}]" :key="item.code" :label="item.name" :value="item.code" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <span class="text-bold text-orange">默认【小于 下限】为阴性,【大于 上限】为阳性</span>
                  </el-col>
                </el-row>
                <el-divider content-position="left">计算项目</el-divider>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="是否计算项目">
                      <el-checkbox v-model="staItemDictForm.isCalcItem" label="是否计算项目" :true-label='1' :false-label="0" :checked-value="1" :unchecked-value="0" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="20">
                    <el-form-item label="计算公式">
                      <el-input v-model="staItemDictForm.calcItemFormula" type="textarea" :disabled="!staItemDictForm.isCalcItem"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-button class="button-blue" :disabled="!staItemDictForm.isCalcItem" @click="toggleItemCalcFormulaDialog('calcItemFormula')">公式向导</el-button>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="20">
                    <el-form-item label="计算公式显示">
                      <el-input v-model="staItemDictForm.calcItemDescription" readonly type="textarea" :disabled="!staItemDictForm.isCalcItem"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-button class="button-blue" :icon="openhidePanel?'ArrowUp':'ArrowDown'" @click="openhidePanel=!openhidePanel">更多公式</el-button>
                  </el-col>
                </el-row>
                <div v-if="openhidePanel">
                  <el-row :gutter="20">
                    <el-col :span="20">
                      <el-form-item label="结果1计算公式">
                        <el-input v-model="staItemDictForm.result1Formula" type="textarea" :disabled="!staItemDictForm.isCalcItem"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-button class="button-blue" :disabled="!staItemDictForm.isCalcItem" @click="toggleItemCalcFormulaDialog('result1Formula')">公式向导</el-button>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="20">
                      <el-form-item label="结果2计算公式">
                        <el-input v-model="staItemDictForm.result2Formula" type="textarea" :disabled="!staItemDictForm.isCalcItem"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-button class="button-blue" :disabled="!staItemDictForm.isCalcItem" @click="toggleItemCalcFormulaDialog('result2Formula')">公式向导</el-button>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="20">
                      <el-form-item label="结果3计算公式">
                        <el-input v-model="staItemDictForm.result3Formula" type="textarea" :disabled="!staItemDictForm.isCalcItem"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-button class="button-blue" :disabled="!staItemDictForm.isCalcItem" @click="toggleItemCalcFormulaDialog('result3Formula')">公式向导</el-button>
                    </el-col>
                  </el-row>
                </div>
              </el-form>
            </el-scrollbar>
            <StaItemDictInfoCalcFormula ref="staItemDictCalcFormulaRef" :type="formulaType" @confirmFormula="confirmFormula" :formula="staItemDictForm[formulaType]" :description="formulaType==='calcItemFormula'?staItemDictForm.calcItemDescription:''" />
          </el-tab-pane>
          <el-tab-pane label="危急值" name="criticalPane" :disabled="isEmpty(staItemDictForm.id)">
            <StaItemDictCritical ref="staCriticalRef" />
          </el-tab-pane>
          <el-tab-pane label="文字结果" name="textResultPane" :disabled="isEmpty(staItemDictForm.id)">
            <StaTextResult ref="textResultRef" />
          </el-tab-pane>
        </el-tabs>
      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import StaItemDictInfoCalcFormula from './StaItemDictInfoCalcFormula.vue';
import StaItemDictCritical from './StaItemDictCritical.vue';
import StaTextResult from './StaTextResult.vue';
import SelectDataList from '@/components/SelectDataList.vue';

defineExpose({ initPage });
let searchForm = {
  searchValue: ''
};
let tloading = ref(false); //加载状态
let activeName = ref('first');
let staItemDictCalcFormulaRef = ref(null);
let staCriticalRef: any = ref(null);
let textResultRef: any = ref(null);
let workGroupList: any = reactive([]); //字典表：指控规则类型

let formulaType = ref('');

let staItemDictList: any = reactive([]);
const itemClassDict = reactive([]); //项目类型字典
let dictTreeTypeCodes: any = reactive(['reportCategory', 'YQDL', 'LABITEMKIND', 'JYGZZ']);

let sltStaItemDict: any = ref({});
// 条数/页
const PageSizeList = [10, 50, 100, 200, 500];
// 分页
let page = reactive({
  currentPage: 1,
  pageSize: PageSizeList[1],
  totalResult: 0
});

let staItemDictForm = reactive({
  id: null,
  itemNo: '',
  itemNa: '',
  itemEnName: '',
  itemEnCode: '',
  inputCode: '',
  basItemCode: '',
  enable: 1,
  itemClass: '',
  workGroup: '',
  instrClass: '',
  itemKind: null,
  decPos: 0,
  unit: '',
  price: '',
  reportCode: '',
  countCode: '',
  defaultVal: '',
  baseCode: '',
  testMethod: '',
  erFeeitemCode: '',
  erFeeitemName: '',
  defCalcValue: '',
  defTextResultFlag: '',
  sampleQuality: '',
  numType: '',
  hisItemCode: '',
  physItemCode: '',
  outResultNum: '',
  outResultDiluFactor: '',
  allowCvPerct: '',
  allowTeaPerct: '',
  workLoad: '',
  isStatistics: 0,
  isReport: 0,
  isQc: 0,
  isEqc: 0,
  isCalcItem: 0,
  calcItemFormula: '',
  calcItemDescription: '',
  result1Formula: '',
  result2Formula: '',
  result3Formula: '',
  lnum: '',
  hnum: '',
  ref: '',
  lflag: '',
  hflag: '',
  isTran: '0',
  remark: '',
  seq: 1,
  orgId: ''
});

let staItemDictFormRule = {
  itemdictTitle: [
    {
      required: true,
      message: '请输入功能名称',
      trigger: 'blur'
    },
    {
      min: 2,
      max: 20,
      message: '长度为2到20个字符',
      trigger: 'blur'
    }
  ],
  itemdictCode: [
    {
      required: true,
      message: '请输入功能编码',
      trigger: 'blur'
    }
  ],
  staItemDictCode: [
    {
      required: true,
      message: '请输入标准功能编号',
      trigger: 'blur'
    }
  ]
};

onMounted(() => {
  getDictItemsList();
});
function initPage() {
  getStaItemDictList();
}

let instrClassDict: any = reactive([]); //检验组option下拉数据
let itemKindDict: any = reactive([]); //检验组option下拉数据
let openhidePanel = ref(false); /**展开隐藏更多公式面板 */

//批量获取字典
async function getDictItemsList() {
  const codes = dictTreeTypeCodes.join(',');
  const url = '/api/sys/basDictDetail/findDictDetailListByDictCodes';
  const dictTreeTypeItems: any = await proxy.$http.get(url, { params: { code: codes } });
  let itemClassType = dictTreeTypeItems.data.find((item) => item.code === 'reportCategory');
  itemClassDict.push(...itemClassType.detailDataList);
  //searchForm.itemClass = itemClassDict[0].code;

  const instrClassType = dictTreeTypeItems.data.find((item) => item.code === 'YQDL');
  instrClassDict.push(...instrClassType.detailDataList);
  //searchForm.instrClass = instrClassDict[0].code;

  const itemKindType = dictTreeTypeItems.data.find((item) => item.code === 'LABITEMKIND');
  itemKindDict.push(...itemKindType.detailDataList);

  const workGroupType = dictTreeTypeItems.data.find((item) => item.code === 'JYGZZ');
  workGroupList.push(...workGroupType.detailDataList);
}

function getStaItemDictList() {
  tloading.value = true;
  staItemDictList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staItemDict/findStaItemDictPageList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue,
        pageIndex: page.currentPage,
        pageSize: page.pageSize
      }
    })
    .then((res: any) => {
      staItemDictList.push(...res.data.records);
      page.totalResult = res.data.total;
      const kn = staItemDictList.find((k) => k.id == sltStaItemDict.value.id);
      if (isEmpty(kn)) {
        itemdictRowClick({ row: staItemDictList[0] }); //默认选中第一条数据
      } else {
        itemdictRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function sysStaItemDict() {
  tloading.value = true;
  staItemDictList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staItemDict/sysStaItemDict';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue,
        pageIndex: page.currentPage,
        pageSize: page.pageSize
      }
    })
    .then((res: any) => {
      staItemDictList.push(...res.data.records);
      page.totalResult = res.data.total;
      const kn = staItemDictList.find((k) => k.id == sltStaItemDict.value.id);
      if (isEmpty(kn)) {
        itemdictRowClick({ row: staItemDictList[0] }); //默认选中第一条数据
      } else {
        itemdictRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

//点击每页数量(分页查询)
function handleSizeChange({ type, currentPage, pageSize }) {
  if (type === 'size') {
    page.currentPage = 1;
  } else {
    page.currentPage = currentPage;
  }
  page.pageSize = pageSize;
  getStaItemDictList();
}

function saveStaItemDict(formName: any) {
  let $el = proxy.$refs[formName];
  $el.validate((valid: any) => {
    if (!valid) {
      return false;
    }
    if (staItemDictForm.id === null) {
      const url = '/lis/sta/staItemDict/addStaItemDict';
      const staItemDict = {
        itemNo: staItemDictForm.itemNo,
        itemNa: staItemDictForm.itemNa,
        itemEnName: staItemDictForm.itemEnName,
        itemEnCode: staItemDictForm.itemEnCode,
        inputCode: staItemDictForm.inputCode,
        basItemCode: staItemDictForm.basItemCode,
        enable: staItemDictForm.enable,
        itemClass: staItemDictForm.itemClass,
        workGroup: staItemDictForm.workGroup,
        instrClass: staItemDictForm.instrClass,
        itemKind: staItemDictForm.itemKind,
        decPos: staItemDictForm.decPos,
        unit: staItemDictForm.unit,
        price: staItemDictForm.price,
        reportCode: staItemDictForm.reportCode,
        countCode: staItemDictForm.countCode,
        defaultVal: staItemDictForm.defaultVal,
        baseCode: staItemDictForm.baseCode,
        testMethod: staItemDictForm.testMethod,
        erFeeitemCode: staItemDictForm.erFeeitemCode,
        defCalcValue: staItemDictForm.defCalcValue,
        defTextResultFlag: staItemDictForm.defTextResultFlag,
        sampleQuality: staItemDictForm.sampleQuality,
        numType: staItemDictForm.numType,
        hisItemCode: staItemDictForm.hisItemCode,
        physItemCode: staItemDictForm.physItemCode,
        outResultNum: staItemDictForm.outResultNum,
        outResultDiluFactor: staItemDictForm.outResultDiluFactor,
        allowCvPerct: staItemDictForm.allowCvPerct,
        allowTeaPerct: staItemDictForm.allowTeaPerct,
        workLoad: staItemDictForm.workLoad,
        isStatistics: staItemDictForm.isStatistics,
        isReport: staItemDictForm.isReport,
        isQc: staItemDictForm.isQc,
        isEqc: staItemDictForm.isEqc,
        isCalcItem: staItemDictForm.isCalcItem,
        calcItemFormula: staItemDictForm.calcItemFormula,
        calcItemDescription: staItemDictForm.calcItemDescription,
        result1Formula: staItemDictForm.result1Formula,
        result2Formula: staItemDictForm.result2Formula,
        result3Formula: staItemDictForm.result3Formula,
        lnum: staItemDictForm.lnum,
        hnum: staItemDictForm.hnum,
        ref: staItemDictForm.ref,
        lflag: staItemDictForm.lflag,
        hflag: staItemDictForm.hflag,
        isTran: staItemDictForm.isTran,
        remark: staItemDictForm.remark,
        seq: staItemDictForm.seq
      };
      proxy.$http
        .post(url, staItemDict)
        .then((res: any) => {
          VxeMessage('新增成功', VxeNotifyType.success);
          clearForm();
          sltStaItemDict.value.id = res.data.id;
          getStaItemDictList();
        })
        .catch((err: any) => {
          VxeMessage('新增失败', VxeNotifyType.error);
        });
    } else {
      // 修改用户
      const url = '/lis/sta/staItemDict/editStaItemDict';

      const staItemDict = {
        id: staItemDictForm.id,
        itemNo: staItemDictForm.itemNo,
        itemNa: staItemDictForm.itemNa,
        itemEnName: staItemDictForm.itemEnName,
        itemEnCode: staItemDictForm.itemEnCode,
        inputCode: staItemDictForm.inputCode,
        basItemCode: staItemDictForm.basItemCode,
        enable: staItemDictForm.enable,
        itemClass: staItemDictForm.itemClass,
        workGroup: staItemDictForm.workGroup,
        instrClass: staItemDictForm.instrClass,
        itemKind: staItemDictForm.itemKind,
        decPos: staItemDictForm.decPos,
        unit: staItemDictForm.unit,
        price: staItemDictForm.price,
        reportCode: staItemDictForm.reportCode,
        countCode: staItemDictForm.countCode,
        defaultVal: staItemDictForm.defaultVal,
        baseCode: staItemDictForm.baseCode,
        testMethod: staItemDictForm.testMethod,
        erFeeitemCode: staItemDictForm.erFeeitemCode,
        defCalcValue: staItemDictForm.defCalcValue,
        defTextResultFlag: staItemDictForm.defTextResultFlag,
        sampleQuality: staItemDictForm.sampleQuality,
        numType: staItemDictForm.numType,
        hisItemCode: staItemDictForm.hisItemCode,
        physItemCode: staItemDictForm.physItemCode,
        outResultNum: staItemDictForm.outResultNum,
        outResultDiluFactor: staItemDictForm.outResultDiluFactor,
        allowCvPerct: staItemDictForm.allowCvPerct,
        allowTeaPerct: staItemDictForm.allowTeaPerct,
        workLoad: staItemDictForm.workLoad,
        isStatistics: staItemDictForm.isStatistics,
        isReport: staItemDictForm.isReport,
        isQc: staItemDictForm.isQc,
        isEqc: staItemDictForm.isEqc,
        isCalcItem: staItemDictForm.isCalcItem,
        calcItemFormula: staItemDictForm.calcItemFormula,
        calcItemDescription: staItemDictForm.calcItemDescription,
        result1Formula: staItemDictForm.result1Formula,
        result2Formula: staItemDictForm.result2Formula,
        result3Formula: staItemDictForm.result3Formula,
        lnum: staItemDictForm.lnum,
        hnum: staItemDictForm.hnum,
        ref: staItemDictForm.ref,
        lflag: staItemDictForm.lflag,
        hflag: staItemDictForm.hflag,
        isTran: staItemDictForm.isTran,
        remark: staItemDictForm.remark,
        seq: staItemDictForm.seq
      };
      proxy.$http
        .post(url, staItemDict)
        .then((res: any) => {
          VxeMessage('修改成功', VxeNotifyType.success);
          sltStaItemDict.value.id = res.data.id;
          setTimeout(() => {
            clearForm();
            getStaItemDictList();
          }, 50);
        })
        .catch((err: any) => {
          VxeMessage('修改失败', VxeNotifyType.error);
        });
    }
  });
}
function deleteStaItemDict() {
  const sltStaItemDicts = (proxy.$refs.staItemDictTableRef as any).getCheckboxRecords();
  if (!(sltStaItemDicts.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        //后台删除数据
        const delStaItemDictList = sltStaItemDicts.filter((p: any) => !isEmpty(p.id));
        if (delStaItemDictList.length > 0) {
          const data = { staItemDictList: delStaItemDictList };
          const url = '/lis/sta/staItemDict/batchDeleteStaItemDictList';
          proxy.$http
            .post(url, data)
            .then((res: any) => {
              getStaItemDictList(); // 获取检验仪器表格数据
              VxeMessage(res.status.msg, VxeNotifyType.success);
            })
            .catch((err: any) => {
              VxeMessage(err, VxeNotifyType.error);
            });
        }
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}
// 回车查询
function keyupEnter(e: any) {
  if (e.$event.code == 'Enter') {
    getStaItemDictList();
  }
}
// 点击设计模式表格当前行触发右侧表单变化
function itemdictRowClick({ row }: any) {
  if (isEmpty(row)) {
    clearForm();
    sltStaItemDict.value = {};
  } else {
    (proxy.$refs.staItemDictTableRef as any).setCurrentRow(row);
    setStaItemDictForm(row);
  }

  if (activeName.value == 'criticalPane') {
    staCriticalRef.value.initPage(staItemDictForm);
  } else if (activeName.value == 'textResultPane') {
    textResultRef.value.initPage(staItemDictForm);
  }
}

function addStaItemDictDialog() {
  clearForm();
  sltStaItemDict.value = {};
  (proxy.$refs.staItemDictTableRef as any).clearCurrentRow();
  activeName.value = 'first';
}

// 清空中间规则弹框表单
function clearForm() {
  staItemDictForm.id = null;
  staItemDictForm.itemNo = '';
  staItemDictForm.itemNa = '';
  staItemDictForm.itemEnName = '';
  staItemDictForm.itemEnCode = '';
  staItemDictForm.inputCode = '';
  staItemDictForm.basItemCode = '';
  staItemDictForm.enable = 1;
  staItemDictForm.itemClass = '';
  staItemDictForm.workGroup = '';
  staItemDictForm.instrClass = '';
  staItemDictForm.itemKind = null;
  staItemDictForm.decPos = 0;
  staItemDictForm.unit = '';
  staItemDictForm.price = '';
  staItemDictForm.reportCode = '';
  staItemDictForm.countCode = ''; //是否为组件
  staItemDictForm.defaultVal = ''; //是否工具栏显示
  staItemDictForm.baseCode = '';
  staItemDictForm.testMethod = '';
  staItemDictForm.erFeeitemCode = '';
  staItemDictForm.erFeeitemName = '';
  staItemDictForm.defCalcValue = '';
  staItemDictForm.defTextResultFlag = '';
  staItemDictForm.sampleQuality = '';
  staItemDictForm.numType = '';
  staItemDictForm.hisItemCode = '';
  staItemDictForm.physItemCode = '';
  staItemDictForm.outResultNum = '';
  staItemDictForm.outResultDiluFactor = '';
  staItemDictForm.allowCvPerct = '';
  staItemDictForm.allowTeaPerct = '';
  staItemDictForm.workLoad = '';
  staItemDictForm.isStatistics = 0;
  staItemDictForm.isReport = 0;
  staItemDictForm.isQc = 0;
  staItemDictForm.isEqc = 0;
  staItemDictForm.isCalcItem = 0;
  staItemDictForm.calcItemFormula = '';
  staItemDictForm.calcItemDescription = '';
  staItemDictForm.result1Formula = '';
  staItemDictForm.result2Formula = '';
  staItemDictForm.result3Formula = '';
  staItemDictForm.lnum = '';
  staItemDictForm.hnum = '';
  staItemDictForm.ref = '';
  staItemDictForm.lflag = '';
  staItemDictForm.hflag = '';
  staItemDictForm.isTran = '0';
  staItemDictForm.remark = '';
  staItemDictForm.seq = 1;
  staItemDictForm.orgId = '';
}

// 编辑当前行规则
function setStaItemDictForm(row: any) {
  staItemDictForm.id = row.id;
  staItemDictForm.itemNo = row.itemNo;
  staItemDictForm.itemNa = row.itemNa;
  staItemDictForm.itemEnName = row.itemEnName;
  staItemDictForm.itemEnCode = row.itemEnCode;
  staItemDictForm.inputCode = row.inputCode;
  staItemDictForm.basItemCode = row.basItemCode;
  staItemDictForm.enable = row.enable;
  staItemDictForm.itemClass = row.itemClass;
  staItemDictForm.workGroup = row.workGroup;
  staItemDictForm.instrClass = row.instrClass;
  staItemDictForm.itemKind = row.itemKind;
  staItemDictForm.decPos = row.decPos;
  staItemDictForm.unit = row.unit;
  staItemDictForm.price = row.price;
  staItemDictForm.reportCode = row.reportCode;
  staItemDictForm.countCode = row.countCode; //是否为组件
  staItemDictForm.defaultVal = row.defaultVal; //是否工具栏显示
  staItemDictForm.baseCode = row.baseCode;
  staItemDictForm.testMethod = row.testMethod;
  staItemDictForm.erFeeitemCode = row.erFeeitemCode;
  staItemDictForm.erFeeitemName = row.erFeeitemName;
  staItemDictForm.defCalcValue = row.defCalcValue;
  staItemDictForm.defTextResultFlag = row.defTextResultFlag;
  staItemDictForm.sampleQuality = row.sampleQuality;
  staItemDictForm.numType = row.numType;
  staItemDictForm.hisItemCode = row.hisItemCode;
  staItemDictForm.physItemCode = row.physItemCode;
  staItemDictForm.outResultNum = row.outResultNum;
  staItemDictForm.outResultDiluFactor = row.outResultDiluFactor;
  staItemDictForm.allowCvPerct = row.allowCvPerct;
  staItemDictForm.allowTeaPerct = row.allowTeaPerct;
  staItemDictForm.workLoad = row.workLoad;
  staItemDictForm.isStatistics = row.isStatistics;
  staItemDictForm.isReport = row.isReport;
  staItemDictForm.isQc = row.isQc;
  staItemDictForm.isEqc = row.isEqc;
  staItemDictForm.isCalcItem = row.isCalcItem;
  staItemDictForm.calcItemFormula = row.calcItemFormula;
  staItemDictForm.calcItemDescription = row.calcItemDescription;
  staItemDictForm.result1Formula = row.result1Formula;
  staItemDictForm.result2Formula = row.result2Formula;
  staItemDictForm.result3Formula = row.result3Formula;
  staItemDictForm.lnum = row.lnum;
  staItemDictForm.hnum = row.hnum;
  staItemDictForm.ref = row.ref;
  staItemDictForm.lflag = row.lflag;
  staItemDictForm.hflag = row.hflag;
  staItemDictForm.isTran = row.isTran;
  staItemDictForm.remark = row.remark;
  staItemDictForm.seq = row.seq;
  staItemDictForm.orgId = row.orgId;
  sltStaItemDict.value = row;
}

function tabHandleClick(tab: any) {
  if (tab.props.name == 'criticalPane') {
    staCriticalRef.value.initPage(staItemDictForm);
  } else if (tab.props.name == 'textResultPane') {
    textResultRef.value.initPage(staItemDictForm);
  }
}

/**
 * 计算公式弹窗
 */
function toggleItemCalcFormulaDialog(t) {
  if (staItemDictCalcFormulaRef.value) {
    formulaType.value = t;
    staItemDictCalcFormulaRef.value?.toggleShowHide({});
  }
}

function formatCommon(arr, value) {
  for (const item of arr) {
    if (item.code == value) {
      return item.name;
    }
  }
}

// 获取是否静态的tag标记(绿/红)
function getIsTagType(row: any) {
  if (row == '1') {
    return 'success';
  } else {
    return 'danger';
  }
}
// 获取是否静态文字(是/否)
function getIsStatusDesc(row: any) {
  if (row == '1') {
    return '是';
  } else {
    return '否';
  }
}
function inputItemCodeCode() {
  staItemDictForm.itemEnName = staItemDictForm.itemNo;
  staItemDictForm.reportCode = staItemDictForm.itemNo;
  staItemDictForm.itemEnCode = staItemDictForm.itemNo;
  staItemDictForm.basItemCode = staItemDictForm.itemNo;
}

// 填写仪器名称 自动生成助记符
function addInputCode() {
  staItemDictForm.inputCode = funBdsJsResult('firstPinYinFun', staItemDictForm.itemNa);
}
//回车事件
function keyupFormEnter(e: any, index: any) {
  if (e.key == 'Enter') {
    if (index == 13) {
      index = 0;
      proxy.$refs[`form${index}`].focus();
    } else if (index == 12) {
      index = 1;
      proxy.$refs[`form${index}`].focus();
    } else {
      proxy.$refs[`form${index + 2}`].focus();
    }
  }
}

/**
 * 公式确认
 */
function confirmFormula(e) {
  staItemDictForm[e.type] = e.formula;
  if (e.type === 'calcItemFormula') {
    staItemDictForm['calcItemDescription'] = e.description;
  }
}
function erFeeitemSelect(row) {
  staItemDictForm.erFeeitemCode = row.feeCode;
  staItemDictForm.erFeeitemName = row.feeName;
}
function clearerFeeitemName() {
  staItemDictForm.erFeeitemCode = '';
  staItemDictForm.erFeeitemName = '';
}

function sliceErFeeitemHandler() {
  let newStr = staItemDictForm.erFeeitemName.slice(0, -1);
  staItemDictForm.erFeeitemName = newStr;
}
</script>

<style lang="scss" scoped>
#staItemDictSeDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .modulTable {
    height: calc(100% - 40px - 30px);
    background-color: $base-white;
  }
}
</style>